﻿package jlib.component.tool.skin.points {
	
	import jlib.component.tool.TransformMode;
	import jlib.component.tool.skin.points.AbstractControlPoint;
	import jlib.component.tool.skin.points.decorator.Decorator;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;

	/**
	 * Transform Handler에 들어가는 포인터들.
	 * @author 박종희
	 */
	public class ControlPoint extends AbstractControlPoint {
		
		private var m_mode:String;
		private var m_decorators:Vector.<Decorator>;
		
		public function ControlPoint(): void 
		{
			m_mode = TransformMode.NONE;
			m_decorators = new Vector.<Decorator>();
			
			if( !stage ) this.addEventListener( Event.ADDED_TO_STAGE , initialize );
			else initialize();
		}
		
		private function initialize(e:Event=null): void
		{
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedDisplay );
			
			this.addEventListener(MouseEvent.ROLL_OVER, onHideMouse );
			this.addEventListener(MouseEvent.ROLL_OUT, onShowMouse );
			stage.addEventListener(MouseEvent.MOUSE_DOWN , onMouseDowns );
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUps );
		}
		
		protected function onRemovedDisplay(event:Event):void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER, onHideMouse );
			this.removeEventListener(MouseEvent.ROLL_OUT, onShowMouse );
			stage.removeEventListener(MouseEvent.MOUSE_DOWN , onMouseDowns );
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUps );
		}
		
		private function onMouseUps(e:MouseEvent): void
		{
			Mouse.show();
			this.addEventListener(MouseEvent.ROLL_OVER, onHideMouse );
			this.addEventListener(MouseEvent.ROLL_OUT, onShowMouse );
		}

		private function onMouseDowns(e:MouseEvent): void
		{
			if(e.target is AbstractControlPoint){
				Mouse.hide();
				this.removeEventListener(MouseEvent.ROLL_OVER, onHideMouse );
				this.removeEventListener(MouseEvent.ROLL_OUT, onShowMouse );
			}
		}

		private function onShowMouse(e:MouseEvent): void
		{
			Mouse.show();
		}

		private function onHideMouse(e:MouseEvent): void
		{
			Mouse.hide();
		}

		override public function addDecorator(decorator:Decorator): void
		{
			m_decorators.push(decorator);
			addChild(decorator);
		}
		
		override public function getMode(): String { return m_mode; }
		
		override public function setMode($value:String): void {
			m_mode = $value;
			for each( var deco:Decorator in m_decorators ) deco.setMode(m_mode);
		}
		
		override public function set rotation(value:Number):void {
			for each( var deco:* in m_decorators ) deco.cursor.rotation = value;
		}
		
		override public function dispose(): void
		{
			for each( var deco:* in m_decorators ) deco.dispose();
			if(parent.contains(this)) parent.removeChild(this); 
		}
		
	}//c
}
